package top.makeprogress.taobaospider.parser;

import com.alibaba.fastjson.JSON;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import top.makeprogress.taobaospider.Entity.BoughtRootBean;
import top.makeprogress.taobaospider.config.TaobaoSpilderConfig;
import top.makeprogress.taobaospider.util.LogPrintUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuyi
 * @create 2019-10-11 20:30
 **/

public class TaoBaoAllOrderParser {

    public static BoughtRootBean boughtMessgeGet(String cookies,Integer pageNum,Integer pageSize) throws IOException {
        Document doc;
        Connection connect = Jsoup.connect(TaobaoSpilderConfig.getUrlAllOrder());
        setHeader(connect);
        connect.data(getDataMap(pageNum, pageSize));
        connect.cookies(getCookieMap(cookies));
        doc = connect.post();
        Elements body = doc.getElementsByTag("Body");
        LogPrintUtils.print(body);
        return JSON.parseObject(body.text(), BoughtRootBean.class);
    }

    private static void setHeader(Connection connect) {
        connect.header("authority","buyertrade.taobao.com");
        connect.header("method","POST");
        connect.header("path","/trade/itemlist/asyncBought.htm?action=itemlist/BoughtQueryAction&event_submit_do_query=1&_input_charset=utf8");
        connect.header("scheme","https");
        connect.header("accept","application/json, text/javascript, */*; q=0.01");
        connect.header("accept-encoding","gzip, deflate, br");
        connect.header("accept-language","zh-CN,zh;q=0.9");
        connect.header("cache-control","no-cache");
        connect.header("content-length","183");
        connect.header("content-type","application/x-www-form-urlencoded; charset=UTF-8");
        connect.header("cookie","swfstore=202787; _uab_collina=157079334707953439781207; cookie2=1e5449576bc14623d85e139e66c609be; t=6b0b23cea84a93ea7c4453dec662687c; _tb_token_=e60f3eeb533f8; cna=2oZ0FBss4kUCAXdtb2ieXAyd; v=0; unb=788897000; uc3=lg2=Vq8l%2BKCLz3%2F65A%3D%3D&vt3=F8dByuDnwruRXYbCJ3I%3D&id2=VAmie0yOtVRK&nk2=F5REN0UrKIZ3NC9eH84%3D; csg=14e5d0d7; lgc=tb1911216_2011; cookie17=VAmie0yOtVRK; dnk=tb1911216_2011; skt=602589008dc1e77e; existShop=MTU3MDc5MjcyNw%3D%3D; uc4=nk4=0%40FY4PZdcLfh1w3M0yjM6EZf1gPz6WCnYchA%3D%3D&id4=0%40VhCZQpmN4AvamIDg5eGqqn6Wx3k%3D; tracknick=tb1911216_2011; _cc_=VFC%2FuZ9ajQ%3D%3D; tg=0; _l_g_=Ug%3D%3D; sg=10a; _nk_=tb1911216_2011; cookie1=UIGgSNG7BytUWaQTEn%2FcZc13iqiDmhmzPHlSrm6ZHo0%3D; mt=ci=1_1; thw=cn; ucn=center; uc1=cookie14=UoTbnV%2BAQXrCRw%3D%3D&lng=zh_CN&cookie16=WqG3DMC9UpAPBHGz5QBErFxlCA%3D%3D&existShop=false&cookie21=UtASsssme%2BBq&tag=8&cookie15=UIHiLt3xD8xYTw%3D%3D&pas=0; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; x5sec=7b2274726164656d616e616765723b32223a2263326563366665393535303130396435663861323339333731356333363965364350374f67653046454f7a636b36626b2b4d483762426f4c4e7a67344f446b334d4441774f7a453d227d; whl=-1%260%260%261570794179724; l=cBLNUAquq2MvoVafBOfZRurza77OXQRfGPNzaNbMiICPOKCJlKHlWZIv_7TvCnhVK62vR3Wm0ejuB8LkByCqnzTdqAdnwN5..; isg=BGxssmJiHZ-ocQmjE59YTXRhPUqeJRDP_Hj4B8avhpeI0Qjb7jDLX1wn8NlMoUgn");
        connect.header("origin","https://buyertrade.taobao.com");
        connect.header("pragma","no-cache");
        connect.header("referer","https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm");
        connect.header("sec-fetch-mode","cors");
        connect.header("sec-fetch-site","same-origin");
        connect.header("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36");
        connect.header("x-requested-with","XMLHttpRequest");
    }

    private static Map<String, String> getDataMap(Integer pageNum, Integer pageSize) {
        Map<String,String> map=new HashMap<>();
        map.put("pageNum",String.valueOf(pageNum));
        map.put("pageSize",String.valueOf(pageSize));
        map.put("queryOrder","desc");
        return map;
    }

    private static Map<String, String> getCookieMap(String cookies) {
        String[] cookieArr = cookies.split(";");
        Map<String,String> cookieMap = new HashMap<>();
        for(String item:cookieArr){
            String[] keyValue = item.trim().split("=");
            cookieMap.put(keyValue[0],keyValue[1]);
        }
        return cookieMap;
    }
}